/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package lsichrome;

/**
 *
 * @author indu.chawla
 */

import com.aliasi.matrix.SvdMatrix;
import com.aliasi.tokenizer.PorterStemmerTokenizerFactory;
import java.io.*; 
import java.util.Arrays;
import java.sql.*;
import java.util.*;
public class Indu_LSIChrome {

    /**
     * @param args the command line arguments
     */
    ArrayList<Double> scorearr1 = new ArrayList<Double>();
    ArrayList<Integer> index111 = new ArrayList<Integer>();
    ArrayList<Integer> index21 = new ArrayList<Integer>();
    ArrayList<Double> scorearr = new ArrayList<Double>();
    ArrayList<Integer> index11 = new ArrayList<Integer>();
    ArrayList<Integer> index2 = new ArrayList<Integer>();
    ArrayList<Integer> index = new ArrayList<Integer>();
    
    private double[][]  array2;
     private double[][] transpose;
    private double[][]  scale2;
    private double[][] LSITERM_TERM_MATRIX;
    private double[][] TERM_TERM_MATRIX;
    private double[][] TERM_DOCUMENT_MATRIX;
    private String[] TERMS;
    private int NUM_FACTORS = 2;
    private String[] DOCS;
    
    public void setTerms(String[] terms){
        //System.arraycopy(TERMS,0,terms,0,(terms.length()-1));
    	this.TERMS = terms;
    }
    
    public void setTermTermMatrix(double[][] tdm_term){
   	this.TERM_TERM_MATRIX = tdm_term;
   }
    
    public void setDocs(String[] docs){
    	this.DOCS = docs;
    }
    
    public void setTermDocumentMatrix(double[][] tdm){
    	this.TERM_DOCUMENT_MATRIX = tdm;
    }
    
     
     
    
    public void process() {
    	
    try{
    	double featureInit = 0.1;
        double initialLearningRate = 0.005;
        int annealingRate = 10;
        double regularization = 0.00;
        double minImprovement = 0.0001;
        int minEpochs = 10;
        int maxEpochs = 50;

        System.out.println("  Computing SVD");
        System.out.println("    maxFactors=" + NUM_FACTORS);
        System.out.println("    featureInit=" + featureInit);
        System.out.println("    initialLearningRate=" + initialLearningRate);
        System.out.println("    annealingRate=" + annealingRate);
        System.out.println("    regularization" + regularization);
        System.out.println("    minImprovement=" + minImprovement);
        System.out.println("    minEpochs=" + minEpochs);
        System.out.println("    maxEpochs=" + maxEpochs);
        
        SvdMatrix matrix
            = SvdMatrix.svd(TERM_DOCUMENT_MATRIX,
			    NUM_FACTORS,
			    featureInit,
			    initialLearningRate,
			    annealingRate,
			    regularization,
                            null,
			    minImprovement,
			    minEpochs,
			    maxEpochs);
        
        double[] scales = matrix.singularValues();
	    double[][] termVectors = matrix.leftSingularVectors();
	    double[][] docVectors = matrix.rightSingularVectors();
        double[][]  scale2 = new double[2][2];
        double[][]  array2 = new double[termVectors.length][2]; 
        double[][]  transpose = new double[2][termVectors.length];
        double[][] LSITERM_TERM_MATRIX = new double[termVectors.length][termVectors.length];
        
        System.out.println("\nSCALES");
	for (int k = 0; k < NUM_FACTORS; ++k)
	    System.out.printf("%d  %4.2f\n",k,scales[k]);
	System.out.print("THESE ARE TERMS");
for (int i = 0; i < termVectors.length; ++i) {
    
    System.out.println(TERMS[i]);
}
	
	System.out.println("\nTERM VECTORS");
	for (int i = 0; i < termVectors.length; ++i) {
	    System.out.print("(");
	    for (int k = 0; k < NUM_FACTORS; ++k) {
		if (k > 0) System.out.print(", ");
		System.out.printf("% 5.2f",termVectors[i][k]);
	    }
	    System.out.print(")  ");
	    System.out.println(TERMS[i]);
	}
	/* for(int t = 0; t<2 ;++t){
                System.out.print(DOCS[t]);
            }*/
	System.out.println("\nDOC VECTORS");
	for (int j = 0; j < docVectors.length; ++j) {
             System.out.print("(");
	    for (int k = 0; k < NUM_FACTORS; ++k) {
		if (k > 0) System.out.print(", ");
		System.out.printf("% 5.2f",docVectors[j][k]);
                  
	    }
	    System.out.print(")  ");
            System.out.println();
	//    System.out.println(DOCS[j]);
            
	}
        
     //To print term-term matrix---
          
         
       for (int i = 0; i < NUM_FACTORS;i++) {
	    
	    for (int j = 0; j < NUM_FACTORS; j++) {
                
                if(i==j)
                    scale2[i][j]=scales[i];
                  else
                    scale2[i][j]=0.0;
            } 
        }
        System.out.println("scales");
        for(int i = 0; i <2; i++) {
            for(int j = 0; j <2; j++) {
              System.out.print( scale2[i][j]+"   ");
            }
          System.out.println();
          }
        
        
        for (int i = 0; i < termVectors.length; ++i) {
	    
	    for (int j = 0; j < 2; ++j) {
                for(int k = 0; k <2; k++){
                array2[i][j]+= termVectors[i][k]*scale2[k][j];
              }
            }
        }
        
        System.out.println("Multiply of both matrix : ");
          for(int i = 0; i <termVectors.length; i++) {
            for(int j = 0; j <2; j++) {
              System.out.printf("% 5.2f", array2[i][j]);
            }
          System.out.println();
          }
          
          for ( int c = 0 ; c < termVectors.length ; c++ )
      {
         for ( int d = 0 ; d < 2 ; d++ )               
            transpose[d][c] = array2[c][d];         
 
      }
          
          System.out.println(" transpose matrix : ");
          for(int i = 0; i <2; i++) {
            for(int j = 0; j <termVectors.length; j++) {
              System.out.printf("% 5.2f", transpose[i][j]);
            }
          System.out.println();
          }

          for (int i = 0; i < termVectors.length; i++) {
	    
	    for (int j = 0; j < termVectors.length; j++) {
                for(int k = 0; k <2; k++){
                LSITERM_TERM_MATRIX[i][j]+= array2[i][k]*transpose[k][j];
              }
            }
        }
          
          System.out.println(" LSI term-term matrix : ");
          for(int i = 0; i <termVectors.length; i++) {
            for(int j = 0; j <termVectors.length; j++) {
              System.out.printf("% 8.5f", LSITERM_TERM_MATRIX[i][j]);
            }
          System.out.println();
          }
          
          System.out.println( "TERM_TERM_MATRIX");
                        for(int k=0;k<termVectors.length;k++){
                            
			for(int m=0;m<termVectors.length;m++){
			System.out.print( TERM_TERM_MATRIX[k][m]  +"    " );
                        } //m for
                       System.out.println("  ");
                        }
         /*               for(int i = 0; i <((termVectors.length)-1); i++) {
            for(int j = i+1; j <((termVectors.length)-1); j++) {
                 
                    if(TERM_TERM_MATRIX[i][j]!=0){
                        
                    scorearr1.add(TERM_TERM_MATRIX[i][j]);
                    index111.add(i);
                    index21.add(j);
                    
                    }    
                }
            }  
                        bubblesort(scorearr1,index111,index21);
        int indexa = scorearr.size();

                        // ENTER THE NUMBER OF Issues to be displayed in result
                        int num = 20;
                        while(indexa > (scorearr.size()-num))
                        {
                            System.out.print(" Score is  " + scorearr.get(indexa-1).floatValue());
                           // System.out.print("   Index of first term is  " + index11.get(index1-1));
                            int r1= index111.get(indexa-1);
                            System.out.print("    "  +TERMS[r1]);
                         //   System.out.println("  Index of second term is  " + index2.get(index1-1));
                            int r2= index21.get(indexa-1);
                            System.out.println(" ,"+ TERMS[r2]);
                            indexa--;
                        }  */
    
                        
                        
                   for(int i = 0; i <((termVectors.length)-1); i++) {
            for(int j = i+1; j <((termVectors.length)-1); j++) {
                 
                    if(TERM_TERM_MATRIX[i][j]==0){
                        System.out.println( LSITERM_TERM_MATRIX[i][j]);
                    scorearr.add(LSITERM_TERM_MATRIX[i][j]);
                    index11.add(i);
                    index2.add(j);
                    
                    }    
                }
            }  
                 bubblesort(scorearr,index11,index2);
        int index1 = scorearr.size();

                        // ENTER THE NUMBER OF Issues to be displayed in result
                        int num1 = 20;
                        while(index1 > (scorearr.size()-num1))
                        {
                            System.out.print(" Score is  " + scorearr.get(index1-1).floatValue());
                           // System.out.print("   Index of first term is  " + index11.get(index1-1));
                            int r1= index11.get(index1-1);
                            System.out.print("    "  +TERMS[r1]);
                         //   System.out.println("  Index of second term is  " + index2.get(index1-1));
                            int r2= index2.get(index1-1);
                            System.out.println(" ,"+ TERMS[r2]);
                            index1--;
                        }  
    
                   

        }catch(Exception e){
    	System.out.println(e);
    }
    	
   }

    
    public static void bubblesort(ArrayList<Double> arr1,ArrayList<Integer> arr2, ArrayList<Integer> arr3)
    {
        int i, j,n=0;
        n = arr1.size();
        double t=0;
        //int t1;
        int t1,t2;
        for(i = 0; i < n; i++)
        {
            for(j = 1; j < (n-i); j++)
            {
                if(arr1.get(j-1) > arr1.get(j))
                {
                    t = arr1.get(j-1);
                    arr1.set(j-1, arr1.get(j));
                    arr1.set(j, t);

                    t1 = arr2.get(j-1);
                    arr2.set(j-1, arr2.get(j));
                    arr2.set(j, t1);
                    
                    t2 = arr3.get(j-1);
                    arr3.set(j-1, arr3.get(j));
                    arr3.set(j, t2);
                    
                     
                }
            }
        }
    }
        
    
    public static void main(String[] args) {
        // TODO code application logic here
        
        try {           
                        String dbUser = "root";
                        String dbPswd = "123";
                        String dbUrl = "jdbc:mysql://localhost/mysql";
                        Connection conn;                      
                                                       
                        
                        
                        Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
                        conn = DriverManager.getConnection( dbUrl, dbUser, dbPswd );                        
                                                                   
                         
 
                        Statement s0 = conn.createStatement();
			s0.executeQuery( "SELECT issueid,title, description FROM chrome.all_bugs limit 0,10");
			ResultSet rs2 = s0.getResultSet();
                         tdm tdm1 = new  tdm();
                        String link=null,link2=null,link3=null,link4=null,link5=null;
                        tdm1.setNumDocuments(10);
                        int index=-1;
                        Vector <String> terms =new Vector <String>();
                        Vector <String> docs =new Vector <String>();
                        while(rs2.next())
                        {
                        
                       
                       String issueid=rs2.getString("issueid");
                       String title= rs2.getString("title");
                       String desc = rs2.getString("description");
                       String component = rs2.getString("title");
                       String Comment = rs2.getString("description");
                                                       
                             preprocess pf = new preprocess();
                             
                             title=pf.clean(title);
                           desc=pf.clean(desc);
                            Comment=pf.clean(Comment);
                            
                        title=pf.remove_SW(title);
                            desc=pf.remove_SW(desc);
                            Comment=pf.remove_SW(Comment);
                            System.out.println("title"   +title);
                                                                             
                            
                                title=pf.remove_stop_words(title);
                                desc=pf.remove_stop_words(desc);
                                Comment=pf.remove_stop_words(Comment);
                                
                            title=pf.apply_stemming(title);
                            desc=pf.apply_stemming(desc);
                             Comment=pf.apply_stemming(Comment);                
                                                      
                              
                                 link = title +" "+ desc;   
                                 System.out.println("link"   +link);
                               System.out.println("========================");                                  
                                              
                         
                     //  if(! (terms.isEmpty())) 
                        terms.clear();
                               
                        //docs.addElement(link);
                       //link5=link5+link;
                       /* Enumeration VVnum=terms.elements();
                System.out.println("terms in vector");
                while( VVnum.hasMoreElements())
                        System.out.print(VVnum.nextElement()+" ");
                        System.out.println();*/
                        
                        //   int index=1;
                       //terms = mytest.getUniqueTerms();
                        
                       // mytest.setNumDocuments(48);
                      
      String delims = "[ ]+";
 
String[] tokens = link.split(delims);
for (int i = 0; i < tokens.length; i++)
 
{terms.addElement(tokens[i]);
System.out.println("tokens:"  + tokens[i]);
}

/*Enumeration VEnum=terms.elements();
                System.out.println("terms in vector");
                while( VEnum.hasMoreElements())
                        System.out.print(VEnum.nextElement()+" ");
                        System.out.println();
   */
index = index +1;  
    System.out.print("index in main"+ index);
    tdm1.update(link,terms,index);
    
     
     
                        } 
                        
                      
                        Vector <String> doc= tdm1.getDocuments();
                        Enumeration VEnum=doc.elements();
                System.out.println("elements in vector");
                while( VEnum.hasMoreElements())
                        System.out.print(VEnum.nextElement()+" ");
                        System.out.println();
                                                
                       
              /*  System.out.println("uniqueterms ");
                while( VVnum.hasMoreElements())
                { System.out.print(VVnum.nextElement()+" ");
                        System.out.println();}*/
                
                        tdm1.processTermDocumentMatrix();
                         Vector <String> just = tdm1.getUniqueTerms();
                        String[] criteria = new String[just.size()];
                        just.toArray(criteria);
                        
                        Indu_LSIChrome lsi= new  Indu_LSIChrome();
                        lsi.setTerms(criteria);
                        lsi.setTermDocumentMatrix( tdm1.getTermDocumentMatrix());
                        lsi.setTermTermMatrix( tdm1.getTermTermMatrix());
                        lsi.process();
        }          
                       
    
                        
                

                catch (Exception ex) 
                {
                	 ex.printStackTrace();		
                }  
    }
}


    
